package com.mobiusws.reports.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.mobiusws.reports.action.DailyFraudDetailAction;
import com.mobiusws.reports.action.DailyFraudSummaryAction;
import com.mobiusws.reports.model.CallType;
import com.mobiusws.reports.model.CallsNodup;
import com.mobiusws.reports.model.DailyFraudDetail;
import com.mobiusws.reports.model.DailyFraudSummary;
import com.mobiusws.reports.model.LoginInfo;
import com.mobiusws.util.JsonUtil;
import com.mobiusws.util.SqlUtil;
import com.mysql.jdbc.PreparedStatement;

@Repository("callsNodupDao")
public class CallsNodupDao {
	@Resource
	private JdbcTemplate dbxJdbc;
	@Resource
	private JdbcTemplate coreJdbc;
	@Resource
	private SqlUtil sqlUtil;
	private Logger logger = Logger.getRootLogger();

	private int cnt;
	private List<?> list;

	private void logStmt(String sql, Object[] param) {
		logger.info(sql);
		StringBuilder logTxtBld = new StringBuilder();
		for (Object para : param) {
			logTxtBld.append(para).append(", ");
		}
		logger.info(logTxtBld.substring(0, logTxtBld.length() - 2));
	}

	private void execQuery(JdbcTemplate jdbc, String sql, Object[] param,
			BeanPropertyRowMapper rowMapper, QueryType queryType) {
		logStmt(sql, param);
		switch (queryType) {
		case INT:
			this.cnt = jdbc.queryForInt(sql, param);
			break;
		case LIST:
			this.list = jdbc.query(sql, param, rowMapper);
			break;
		}
	}

	public long getMsisdnByImsi(
			String imsi, String startDate, String endDate) {
			String sql = sqlUtil.getSqlById("getMsisdnByImsi");
			Object[] params = new Object[] { startDate, endDate, imsi };
			System.out.println("startDate="+startDate+", endDate="+endDate+", imsi="+imsi);
			BeanPropertyRowMapper rowMapper=new BeanPropertyRowMapper(CallsNodup.class);
			execQuery(coreJdbc, sql, params, rowMapper, QueryType.LIST);
			return list.isEmpty() ? 0 : ((CallsNodup)(this.list.get(0))).getSmsisdn();
	}
	

}
